package com.Demo;

import com.MyImage.MyImage;
import com.component.BackGroundPanel;
import com.util.ScreenUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

public class MainInterface {
    JFrame jf = new JFrame("扶贫农产品销售系统");

    final int WIDTH = 650;
    final int HEIGHT = 400;

    //组装视图
    public void init() throws Exception {
        //设置窗口相关的属性
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        jf.setResizable(false);
        //获取图片路径并设置窗口小图标
        URL imageurl = MyImage.class.getResource("logo.png");
        ImageIcon imageIcon = new ImageIcon(imageurl);
        Image img = imageIcon.getImage();
        jf.setIconImage(img);

        //设置窗口的内容

        URL url = MyImage.class.getResource("FarmingSystemGUI.jpg");
        ImageIcon imageIcon1 = new ImageIcon(url);
        Image img1 = imageIcon1.getImage();
        BackGroundPanel bgPanel = new BackGroundPanel(img1);
        bgPanel.setBounds(0,0,WIDTH,HEIGHT);
        //组装登录相关的元素
        Box vBox = Box.createVerticalBox();

        //组装用户名
        Box uBox = Box.createHorizontalBox();
        JLabel uLabel = new JLabel("用户名：");
        JTextField uField = new JTextField(15);

        uBox.add(uLabel);
        uBox.add(Box.createHorizontalStrut(25));
        uBox.add(uField);

        //组装密码
        Box pBox = Box.createHorizontalBox();
        JLabel pLabel = new JLabel("密    码：");
        JTextField pField = new JTextField(15);

        pBox.add(pLabel);
        pBox.add(Box.createHorizontalStrut(25));
        pBox.add(pField);

        //组装用户类型
        Box tBox = Box.createHorizontalBox();
        JLabel tLabel = new JLabel("用户类型：");
        String[] userTypes = {"消费者", "农户", "管理员"};
        JComboBox<String> userTypeComboBox = new JComboBox<>(userTypes);
        userTypeComboBox.setAlignmentX(Component.CENTER_ALIGNMENT);

        tBox.add(tLabel);
        tBox.add(Box.createHorizontalStrut(15));
        tBox.add(userTypeComboBox);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton loginBtn = new JButton("登录");
        JButton registBtn = new JButton("注册");


        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //loginButtonClicked();
            }
        });

        registBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //registerButtonClicked();
                //跳转到注册页面
                try {
                    new RegisterInterface().init();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                //当前界面消失
                jf.dispose();
            }
        });

        btnBox.add(loginBtn);
        btnBox.add(Box.createHorizontalStrut(100));
        btnBox.add(registBtn);

        vBox.add(Box.createVerticalStrut(70));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(40));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(40));
        vBox.add(tBox);
        vBox.add(Box.createVerticalStrut(50));
        vBox.add(btnBox);

        bgPanel.add(vBox);
        jf.add(bgPanel);
        jf.setVisible(true);


    }

    //客户端程序的入口
    public static void main(String[] args) {
        try {
            new MainInterface().init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
